package com.envocabulary.widget_list;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.database.SQLException;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService.RemoteViewsFactory;

import com.envocabulary.Model.DatabaseController;
import com.envocabulary.Model.Phrases;
import com.envocabulary.Model.Verbs;
import com.envocabulary.Model.Word;
import com.envocabulary.Ultis.Reference;

public class MyRemoteViewsFactory implements RemoteViewsFactory {

	private Context mContext;
	private int mAppWidgetId;
	private static String tag = "MyRemoteViewsFactory";
	private DatabaseController mDB;
	private List<Word> words;
	private List<Verbs> verbs;
	private List<Phrases> phrases;

	public MyRemoteViewsFactory(Context context, Intent intent) {
		mContext = context;
		mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);

		words = new ArrayList<Word>();

		Log.d(tag, "factory creted");
		mDB = new DatabaseController(context);
		try {

			mDB.createDatabase();

		} catch (IOException ioe) {

			throw new Error("Unable to create database");

		}

		try {

			mDB.openDatabase();

		} catch (SQLException sqle) {

			throw sqle;

		}

	}

	// Goi khi factory lan dau tien chay
	@Override
	public void onCreate() {
		Log.d(tag, "onCreate called for widget id:" + mAppWidgetId);
		words = mDB.getRandomWords(5);
	}

	// goi khi notifyDataSetChanged() duoc ban ra tren remote adapter.
	// method nay cho phep RemoteViewFactory dap ung den du lieu update bat ky
	// reference nao ben trong
	@Override
	public void onDataSetChanged() {
		Log.d(tag, "onDataSetChanged");
		if (Reference.TYPE_WORK == 1) {
			words = mDB.getRandomWords(5);
		}

		if (Reference.TYPE_WORK == 2) {
			phrases = mDB.getRandomPharses(5);
		}

		if (Reference.TYPE_WORK == 3) {
			verbs = mDB.getRandomVerbs(5);
		}
		mDB.close();
	}

	// Goi khi RemoteViewsAdapter cuoi cung lien ket voi factory nay unbound
	@Override
	public void onDestroy() {
		Log.d(tag, "destroy called for widget id:" + mAppWidgetId);
	}

	// Tong o item trong list
	@Override
	public int getCount() {
		return 5;
	}

	@Override
	public RemoteViews getViewAt(int position) {

		Log.d(tag, "getview called:" + position);
		Log.e("setting", " " + Reference.TYPE_WORK);
		RemoteViews rv = null;
		if (Reference.TYPE_WORK == 1) {
			rv = new RemoteViews(this.mContext.getPackageName(),
					R.layout.item_listview);

			Word mWord = words.get(position);
			rv.setViewVisibility(R.id.item_text_part, View.GONE);
			rv.setViewVisibility(R.id.item_text_past_simp, View.GONE);
			rv.setTextViewText(R.id.item_text_meaning, mWord.sMeanings);
			rv.setTextViewText(R.id.item_text_phonetic, mWord.sPhonetic);
			rv.setTextViewText(R.id.item_text_word, mWord.sWord);
			this.loadItemOnClickExtras(rv, position);
			return rv;
		}
		if (Reference.TYPE_WORK == 2) {
			rv = new RemoteViews(this.mContext.getPackageName(),
					R.layout.item_listview);

			Phrases mWord = phrases.get(position);
			rv.setViewVisibility(R.id.item_text_part, View.GONE);
			rv.setViewVisibility(R.id.item_text_past_simp, View.GONE);
			rv.setTextViewText(R.id.item_text_phonetic, mWord.vietnamese);
			rv.setTextViewText(R.id.item_text_word, mWord.english);
			this.loadItemOnClickExtras(rv, position);
			return rv;
		}
		if (Reference.TYPE_WORK == 3) {
			rv = new RemoteViews(this.mContext.getPackageName(),
					R.layout.item_listview);

			Verbs mWord = verbs.get(position);
			rv.setTextViewText(R.id.item_text_past_simp, "Quá khứ đơn: "
					+ mWord.past_simple);
			rv.setTextViewText(R.id.item_text_part, "Phân từ quá khứ: "
					+ mWord.past_participle);
			rv.setTextViewText(R.id.item_text_phonetic, mWord.vi_content);
			rv.setTextViewText(R.id.item_text_word, mWord.infinitive);
			rv.setTextViewText(R.id.item_text_meaning, "Ví dụ: "
					+ mWord.example);
			this.loadItemOnClickExtras(rv, position);
			return rv;
		}

		return rv;
	}

	private void loadItemOnClickExtras(RemoteViews rv, int position) {
		// Intent ei = new Intent(mContext, DetailActivity.class);
		// ei.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_TEXT,
		// words.get(position).sWord);
		// rv.setOnClickFillInIntent(R.id.item_text_word, ei);
		// rv.setOnClickFillInIntent(R.id.item_text_meaning, ei);
		// rv.setOnClickFillInIntent(R.id.item_text_phonetic, ei);
		if (Reference.TYPE_WORK == 1) {
			Word word = words.get(position);

			Intent onSettingIntent = new Intent(mContext, DetailActivity.class);
			onSettingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			onSettingIntent.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_TEXT,
					word.sWord);
			onSettingIntent.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_ID,
					word.idWord);
			onSettingIntent
					.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_PHONETIC,
							word.sPhonetic);
			onSettingIntent.putExtra(
					ListWidgetProvider.EXTRA_LIST_ITEM_MEANING, word.sMeanings);

			rv.setOnClickFillInIntent(R.id.item_text_word, onSettingIntent);
			rv.setOnClickFillInIntent(R.id.item_text_meaning, onSettingIntent);
			rv.setOnClickFillInIntent(R.id.item_text_phonetic, onSettingIntent);

		}
		if (Reference.TYPE_WORK == 2) {
			Phrases word = phrases.get(position);

			Intent onSettingIntent = new Intent(mContext, DetailActivity.class);
			onSettingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			onSettingIntent.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_TEXT,
					word.english);
			onSettingIntent.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_ID, "");
			onSettingIntent.putExtra(
					ListWidgetProvider.EXTRA_LIST_ITEM_PHONETIC, "");
			onSettingIntent
					.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_MEANING,
							word.vietnamese);

			rv.setOnClickFillInIntent(R.id.item_text_word, onSettingIntent);
			rv.setOnClickFillInIntent(R.id.item_text_meaning, onSettingIntent);
			rv.setOnClickFillInIntent(R.id.item_text_phonetic, onSettingIntent);

		}
		if (Reference.TYPE_WORK == 3) {
			Verbs word = verbs.get(position);

			Intent onSettingIntent = new Intent(mContext, DetailActivity.class);
			onSettingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			onSettingIntent.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_TEXT,
					 word.infinitive);
			onSettingIntent.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_ID,
					"Phân từ quá khứ: " + word.past_participle);
			onSettingIntent.putExtra(
					ListWidgetProvider.EXTRA_LIST_ITEM_PHONETIC, word.vi_content);
			onSettingIntent
					.putExtra(ListWidgetProvider.EXTRA_LIST_ITEM_MEANING,
							word.example);
			onSettingIntent.putExtra(
					ListWidgetProvider.EXTRA_LIST_ITEM_PAST_SIMPLE,"Quá khứ đơn: "+
					word.past_simple);

			rv.setOnClickFillInIntent(R.id.item_text_word, onSettingIntent);
			rv.setOnClickFillInIntent(R.id.item_text_meaning, onSettingIntent);
			rv.setOnClickFillInIntent(R.id.item_text_phonetic, onSettingIntent);
			rv.setOnClickFillInIntent(R.id.item_text_past_simp, onSettingIntent);
			rv.setOnClickFillInIntent(R.id.item_text_part, onSettingIntent);

		}

		return;
	}

	// loading xuat hien giua luc getViewAt(int) duoc goi va tra ve.
	// mac dinh la null la loading view mac dinh
	@Override
	public RemoteViews getLoadingView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 1;
	}

	// id internal cua item o vi tri position
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	// return true neu cung mot id se tham chieu den cung mot doi tuong
	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

}
